/*
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */
package village_master.FirstClassChange;

import java.util.HashMap;
import java.util.Map;

import net.sf.l2j.gameserver.model.actor.L2Npc;
import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;
import net.sf.l2j.gameserver.model.quest.Quest;
import net.sf.l2j.gameserver.model.quest.QuestState;

/**
 * 
 * @author fernandopm
 */

public class FirstClassChange extends Quest
{
	//Quest Items Dark Elf
	private static final int GAZE_OF_ABYSS = 1244;
	private static final int IRON_HEART = 1252;
	private static final int JEWEL_OF_DARKNESS = 1261;
	private static final int ORB_OF_ABYSS = 1270;
	
	//Quest Items Orcs
	private static final int MARK_OF_RAIDER = 1592;
	private static final int KHAVATARI_TOTEM = 1615;
	private static final int MASK_OF_MEDIUM = 1631;
	
	//Quest Items Dwarf
	private static final int ARTI_MARKS = 1635;
	private static final int SCAV_MARKS = 1642;
	
	//Quest Items Elven Elfs
	private static final int ELVEN_KNIGHT_BROOCH = 1204;
	private static final int REORIA_RECOMMENDATION = 1217;
	private static final int ETERNITY_DIAMOND = 1230;
	private static final int LEAF_OF_ORACLE = 1235;
	
	//Quest Items Human
	private static final int MEDALLION_OF_WARRIOR = 1145;
	private static final int SWORD_OF_RITUAL = 1161;
	private static final int BEZIQUES_RECOMMENDATION = 1190;
	private static final int BEAD_OF_SEASON = 1292;
	private static final int MARK_OF_FAITH = 1201;
	
	//Reward Item
	private static final int SHADOW_WEAPON_COUPON_DGRADE = 8869;
	
	//Classes
	Map <String, int[]> Classes = new HashMap <String, int[]>();
	{
		//Dark Elf
		Classes.put("PK", new int[] { 32, 31, 2, 15, 16, 17, 18, GAZE_OF_ABYSS, 33 });
		Classes.put("AS", new int[] { 35, 31, 2, 19, 20, 21, 22, IRON_HEART, 33 });
		Classes.put("DW", new int[] { 39, 38,2, 23, 24, 25, 26, JEWEL_OF_DARKNESS, 33 });
		Classes.put("SO", new int[] { 42, 38, 2, 27, 28, 29, 30, ORB_OF_ABYSS, 33 });
		
		//Orc
		Classes.put("OR", new int[] { 45, 44, 3, 9, 10, 11, 12, MARK_OF_RAIDER, 23 });
		Classes.put("OM", new int[] { 47, 44, 3, 13, 14, 15, 16, KHAVATARI_TOTEM, 23 });
		Classes.put("OS", new int[] { 50, 49, 3, 17, 18, 19, 20, MASK_OF_MEDIUM, 23 });
		
		//Dwarf
		Classes.put("SC", new int[] { 54, 53, 4, 5, 6, 7, 8, SCAV_MARKS, 11 });
		Classes.put("AR", new int[] { 56, 53, 4, 5, 6, 7, 8, ARTI_MARKS, 11 });
		
		//Elven Elf
		Classes.put("EK", new int[] { 19, 18, 1, 18, 19, 20, 21, ELVEN_KNIGHT_BROOCH, 40 });
		Classes.put("ES", new int[] { 22, 18, 1, 22, 23, 24, 25, REORIA_RECOMMENDATION, 40 });
		Classes.put("EW", new int[] { 26, 25, 1, 15, 16, 17, 18, ETERNITY_DIAMOND, 33 });
		Classes.put("EO", new int[] { 29, 25, 1, 19, 20, 21, 22, LEAF_OF_ORACLE, 33 });
		
		//Human
		Classes.put("HW", new int[] { 1, 0, 0, 26, 27, 28, 29, MEDALLION_OF_WARRIOR, 40 });
		Classes.put("HK", new int[] { 4, 0, 0, 30, 31, 32, 33, SWORD_OF_RITUAL, 40 });
		Classes.put("HR", new int[] { 7, 0, 0, 34, 35, 36, 37, BEZIQUES_RECOMMENDATION, 40 });
		Classes.put("HWI", new int[] { 11, 10, 0, 23 , 24 , 25, 26 , BEAD_OF_SEASON, 33 }); //human wizzard
		Classes.put("HC", new int[] { 15, 10, 0, 27 , 28 , 29 , 30 , MARK_OF_FAITH, 33 });
	}
	
	//Method for Class change
	private void changeClass(QuestState st, L2PcInstance player, int newclass, int item)
	{
		st.takeItems(item, 1);
		st.playSound("ItemSound.quest_fanfare_2");
		player.setClassId(newclass);
		player.setBaseClass(newclass);
		player.broadcastUserInfo();
		return;
	}
	
	public FirstClassChange(int questId, String name, String descr)
	{
		super(questId,name,descr);
		
		addStartNpc(30290, 30297, 30462, 30500, 30505, 30508, 30504, 30595, 30499, 30503, 30594, 30498, 32092, 32093, 30066, 30288, 30373, 30070, 30289, 30037);
		addTalkId(30290, 30297, 30462, 30500, 30505, 30508, 30504, 30595, 30499, 30503, 30594, 30498, 32092, 32093, 30066, 30288, 30373, 30070, 30289, 30037);
	}
	
	@Override
	public String onAdvEvent(String event, L2Npc npc, L2PcInstance player)
	{
		String htmltext = event;
		QuestState st = player.getQuestState("FirstClassChange");
		if (st == null)
			return htmltext;
		String suffix = "";
		int npcId = npc.getNpcId();
		
		if (Classes.containsKey(event))
		{
			int newclass = Classes.get(event)[0];
			int req_class = Classes.get(event)[1];
			int req_race = Classes.get(event)[2];
			int low_ni = Classes.get(event)[3];
			int low_i = Classes.get(event)[4];
			int ok_ni = Classes.get(event)[5];
			int ok_i = Classes.get(event)[6];
			int req_item = Classes.get(event)[7];
			int denied_class = Classes.get(event)[8];
			
			if (player.getRace().ordinal() == req_race && player.getClassId().getId() == req_class)
			{
				int item = st.getQuestItemsCount(req_item);
				
				if (player.getLevel() < 20)
				{
					if (item == 1)
						suffix = "-"+ low_i;
					else if (item == 0)
						suffix = "-"+low_ni;
				}
				else
				{
					if (item == 0)
						suffix = "-"+ok_ni;
					else
					{
						suffix = "-"+ok_i;
						st.giveItems(SHADOW_WEAPON_COUPON_DGRADE, 15);
						changeClass(st,player,newclass,req_item);
					}
				}
				
				htmltext = npcId + suffix + ".htm";
				st.exitQuest(true);
			}
			else
				htmltext = npcId +"-"+denied_class+".htm";
		}
		
		return htmltext;
	}
	
	@Override
	public String onTalk(L2Npc npc, L2PcInstance player)
	{
		String htmltext = getNoQuestMsg(player);
		QuestState st = player.getQuestState("FirstClassChange");
		if (st == null)
			return htmltext;
		
		int npcId = npc.getNpcId();
		int npcId2 = npc.getNpcId();
		
		if (player.isSubClassActive())
		{
			st.exitQuest(true);
			return htmltext;
		}
		
		switch (player.getRace())
		{
		case DarkElf:
			if (npcId2 == 30290 || npcId2 == 30297 || npcId2 == 30462)
			{
				if (player.getClassId().level() == 0)
				{
					if (player.getClassId().getId() == 31)
						htmltext = npcId + "-01.htm";
					else if (player.getClassId().getId() == 38)
						htmltext = npcId + "-08.htm";
				}
				else if (player.getClassId().level() == 1)
					htmltext = npcId + "-32.htm";
				else
					htmltext = npcId + "-31.htm";
			}
			else
			{
				switch (npc.getNpcId())
				{
				//Dwarf
				case 30503:
				case 30594:
				case 30498:
				case 32092:
				case 32093:
				case 30504:
				case 30595:
				case 30499:
					htmltext = npcId + "-11.htm";
					break;
				//Human and Elfs mages
				case 30070:
				case 30037: 
				case 30289:
					htmltext = npcId + "-33.htm";
					break;
				//Human and Elfs fighters
				case 30373:
				case 30288:
				case 30066:
					htmltext = npcId + "-40.htm";
					break;					
				//Orcs
				case 30500:
				case 30505:
				case 30508:
					htmltext = npcId + "-23.htm";
					break;
				}
			}
			break;
			
		case Orc:
			if (npcId2 == 30500 || npcId2 == 30505 || npcId2 == 30508)
			{
				if (player.getClassId().level() == 0)
				{
					if (player.getClassId().getId() == 44)
						htmltext = npcId + "-01.htm";
					else if (player.getClassId().getId() == 49)
						htmltext = npcId + "-06.htm";
				}
				else if (player.getClassId().level() == 1)
					htmltext = npcId + "-21.htm";
				else
					htmltext = npcId + "-22.htm";
			}
			else
			{
				switch (npc.getNpcId())
				{
				//Dwarf
				case 30503:
				case 30594:
				case 30498:
				case 32092:
				case 32093:
				case 30504:
				case 30595:
				case 30499:
					htmltext = npcId + "-11.htm";
					break;
				//Human and Elfs mages
				case 30070:
				case 30037: 
				case 30289:
					htmltext = npcId + "-33.htm";
					break;
				//Human and Elfs fighters
				case 30373:
				case 30288:
				case 30066:
					htmltext = npcId + "-40.htm";
					break;
				//Dark Elfs
				case 30290:
				case 30297:
				case 30462:
					htmltext = npcId +"-33.htm";
					break;
				}
			}
			break;
			
		case Dwarf:
			if (npcId2 == 30503 || npcId2 == 30594 || npcId2 == 30498 || npcId2 == 32092 || npcId2 == 32093 || npcId2 == 30504 || npcId2 == 30595 || npcId2 == 30499)
			{
				if (player.getClassId().level() == 0)
				{
					if (player.getClassId().getId() == 53)
						htmltext = npcId + "-01.htm";
					}
				else if (player.getClassId().level() == 1)
					htmltext = npcId + "-09.htm";
				else
					htmltext = npcId + "-10.htm";
			}
			else
			{
				switch (npc.getNpcId())
				{
				//Orcs
				case 30500:
				case 30505:
				case 30508:
					htmltext = npcId + "-23.htm";
					break;
				//Human and Elfs mages
				case 30070:
				case 30037: 
				case 30289:
					htmltext = npcId + "-33.htm";
					break;
				//Human and Elfs fighters
				case 30373:
				case 30288:
				case 30066:
					htmltext = npcId + "-40.htm";
					break;
				//Dark Elfs
				case 30290:
				case 30297:
				case 30462:
					htmltext = npcId +"-33.htm";
					break;
				}
			}
			break;
			
		case Elf:
			if (player.isMageClass())
			{
				if (npcId2 == 30070 || npcId2 == 30289 || npcId2 == 30037)
				{
					if (player.isMageClass())
					{
						if (player.getClassId().level() == 0)
						{
							if (player.getClassId().getId() == 25)
								htmltext = npcId + "-01.htm";
						}
						else if (player.getClassId().level() == 1)
							htmltext = npcId + "-31.htm";
						else
							htmltext = npcId + "-32.htm";
					}
				}
				else
				{
					switch (npc.getNpcId())
					{
					//Dwarf
					case 30503:
					case 30594:
					case 30498:
					case 32092:
					case 32093:
					case 30504:
					case 30595:
					case 30499:
						htmltext = npcId + "-11.htm";
						break;
					//Orcs
					case 30500:
					case 30505:
					case 30508:
						htmltext = npcId + "-23.htm";
						break;
					//Human and Elfs fighters
					case 30373:
					case 30288:
					case 30066:
						htmltext = npcId + "-40.htm";
						break;
					//Dark Elfs
					case 30290:
					case 30297:
					case 30462:
						htmltext = npcId +"-33.htm";
						break;
					}
				}
			}
			else
			{
				if (npcId2 == 30373 || npcId2 == 30288 || npcId2 == 30066)
				{
					if (player.getClassId().level() == 0)
					{
						if (player.getClassId().getId() == 18)
							htmltext = npcId + "-01.htm";
					}
					else if (player.getClassId().level() == 1)
						htmltext = npcId + "-38.htm";
					else
						htmltext = npcId + "-39.htm";
				}
				else
				{
					switch (npc.getNpcId())
					{
					//Dwarf
					case 30503:
					case 30594:
					case 30498:
					case 32092:
					case 32093:
					case 30504:
					case 30595:
					case 30499:
						htmltext = npcId + "-11.htm";
						break;
					//Orcs
					case 30500:
					case 30505:
					case 30508:
						htmltext = npcId + "-23.htm";
						break;
					//Human and Elfs mages
					case 30070:
					case 30037: 
					case 30289:
						htmltext = npcId + "-33.htm";
						break;
					//Dark Elfs
					case 30290:
					case 30297:
					case 30462:
						htmltext = npcId +"-33.htm";
						break;
					}
				}
			}
			break;
			
		case Human:
			if (player.isMageClass())
			{
				if (npcId2 == 30070 || npcId2 == 30289 || npcId2 == 30037)
				{
					if (player.getClassId().level() == 0)
					{
						if (player.getClassId().getId() == 10)
							htmltext = npcId + "-08.htm";
					}
					else if (player.getClassId().level() == 1)
						htmltext = npcId + "-31.htm";
					else
						htmltext = npcId + "-32.htm";
				}
				else
				{
					switch (npc.getNpcId())
					{
					//Dwarf
					case 30503:
					case 30594:
					case 30498:
					case 32092:
					case 32093:
					case 30504:
					case 30595:
					case 30499:
						htmltext = npcId + "-11.htm";
						break;
					//Orcs
					case 30500:
					case 30505:
					case 30508:
						htmltext = npcId + "-23.htm";
						break;
					//Human and Elfs fighters
					case 30373:
					case 30288:
					case 30066:
						htmltext = npcId + "-40.htm";
						break;
					//Dark Elfs
					case 30290:
					case 30297:
					case 30462:
						htmltext = npcId +"-33.htm";
						break;
					}
				}
			}
			else
			{
				if (npcId2 == 30373 || npcId2 == 30288 || npcId2 == 30066)
				{
					if (player.getClassId().level() == 0)
					{
						if (player.getClassId().getId() == 0)
							htmltext = npcId + "-08.htm";
					}
					else if (player.getClassId().level() == 1)
						htmltext = npcId + "-38.htm";
					else
						htmltext = npcId + "-39.htm";
				}
				else
				{
					switch (npc.getNpcId())
					{
					//Dwarf
					case 30503:
					case 30594:
					case 30498:
					case 32092:
					case 32093:
					case 30504:
					case 30595:
					case 30499:
						htmltext = npcId + "-11.htm";
						break;
					//Orcs
					case 30500:
					case 30505:
					case 30508:
						htmltext = npcId + "-23.htm";
						break;
					//Human and Elfs mages
					case 30070:
					case 30037: 
					case 30289:
						htmltext = npcId + "-33.htm";
						break;
					//Dark Elfs
					case 30290:
					case 30297:
					case 30462:
						htmltext = npcId +"-33.htm";
						break;
					}
				}
			}
			break;
		}
		
		st.exitQuest(true);
		
		return htmltext;
	}
	
	public static void main(String[] args)
	{
		new FirstClassChange(-1, "FirstClassChange", "village_master");
	}
}